TREES
Photo by Avel Chuklanov on Unsplash
History will judge us by the difference we make in the everyday lives of children…
— Nelson Mandela
Every single country has seen a reduction in child deaths (since 1950). Under five mortality rate (% of children that die before they are 5 years old)
# Load data
df = read.csv("./archetypes/reduced-child-deaths/reduced-child-deaths.csv", header = TRUE, stringsAsFactors = FALSE) # read text file
df
df_wrangle <- df
df_wrangle$X..change.1950.2015 <- as.numeric(sub("%", "", df_wrangle$X..change.1950.2015))
df_wrangle$X..change.1950.2015 <- as.numeric(sub("-", "", df_wrangle$X..change.1950.2015))
df_wrangle$un_region <- countrycode(df_wrangle$under_five_mortality_rate, origin='country.name', destination='un.region.name')
colnames(df_wrangle) <- c("COUNTRY", "MEASURE", "REGION")
df_wrangle <- filter(df_wrangle, nchar(REGION) > 0)
df_wrangle
The within function is used for calculating new columns. The quantile function calculates the quartiles where 0:4/4 evaluates to c(0, 0.25, 0.50, 0.75, 1). Finally the cut function splits the data into the calculated quartiles. Casting the result to integers returns groups labeled as 1,2,3,4. The column is converted to a factor for use with a discrete color scale.
df_analysis <- within(df_wrangle, quartile <- factor(as.integer(cut(MEASURE, quantile(MEASURE, probs=0:4/4), include.lowest=TRUE))))
df_analysis
# Layouts
# squarified" (the default), "scol", "srow" or "fixed"
# Simple
v1 <- ggplot(df_analysis, aes(area = MEASURE, fill = quartile)) +
geom_treemap(color = "#ffffff", layout = 'fixed') +
scale_fill_brewer(palette = "PiYG") +
theme_minimal() +
theme(legend.position = "bottom")
girafe(ggobj = v1, width_svg = 1280/72, height_svg = 720/72,
options = list(opts_sizing(rescale = TRUE, width = 1.0))
)
# Labeled
v2 <- ggplot(df_analysis, aes(area = MEASURE, fill = quartile, label = COUNTRY)) +
geom_treemap(color = "#ffffff", layout = 'fixed') +
geom_treemap_text(fontface = "italic", colour = "white", place = "centre", grow = TRUE, layout = 'fixed') +
scale_fill_brewer(palette = "PiYG") +
theme_minimal() +
theme(legend.position = "bottom")
girafe(ggobj = v2, width_svg = 1280/72, height_svg = 720/72,
options = list(opts_sizing(rescale = TRUE, width = 1.0))
)